package org.getOffer.offer43;

/**
 * @Author: wjy
 * @Date: 2021/12/3 9:14
 */
public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();
        System.out.println(solution.countDigitOne(29));

    }

    /**
     * todo 实在没tm搞懂
     * @param n
     * @return
     */
    public int countDigitOne(int n) {
        // digit：记录 个、十、百、千。。。等位数 res：记录结果
        int digit = 1, res = 0;
        // high：记录高位 cur：记录中位 low：记录低位 如：当n=1234时，
        int high = n / 10, cur = n % 10, low = 0;
        while(high != 0 || cur != 0) {
            if(cur == 0) res += high * digit;
            else if(cur == 1) res += high * digit + low + 1;
            else res += (high + 1) * digit;
            low += cur * digit;
            cur = high % 10;
            high /= 10;
            digit *= 10;
        }
        return res;
    }

}
